//
//  CYFinanceMemberViewController.m
//  cyjf
//
//  Created by tauCross on 2017/5/8.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYFinanceMemberViewController.h"

#import "CYFinanceMemberCell.h"



@interface CYFinanceMemberViewController ()

@property(nonatomic, strong)UILabel *memberLabel;
@property(nonatomic, strong)UILabel *amountLabel;
@property(nonatomic, strong)UILabel *dateLabel;

@property(nonatomic, assign)NSInteger page_no;
@property(nonatomic, assign)BOOL loading;
@property(nonatomic, assign)BOOL hasMore;

@property(nonatomic, assign)BOOL errorShowed;

@end


@implementation CYFinanceMemberViewController

- (void)loadUI
{
    self.navigationItem.title = @"投资记录";
    
    self.tableView.backgroundColor = [UIColor clearColor];
    
    self.memberLabel = [self titleLabelWithText:@"投资用户"];
    [self.contentView addSubview:self.memberLabel];
    
    self.amountLabel = [self titleLabelWithText:@"投资金额"];
    [self.contentView addSubview:self.amountLabel];
    
    self.dateLabel = [self titleLabelWithText:@"投资时间"];
    [self.contentView addSubview:self.dateLabel];
}

- (void)loadData
{
    self.page_no = 1;
    self.loading = NO;
    self.hasMore = YES;
    
    self.tableModel = [[NIMutableTableViewModel alloc] initWithSectionedArray:@[] delegate:(id)[NICellFactory class]];
    self.tableView.dataSource = self.tableModel;
    [self.tableView reloadData];
    
    [self loadDataFromServer];
}

- (void)layoutUI
{
    self.memberLabel.left = 20;
    self.memberLabel.centerY = 23;
    
    self.dateLabel.right = self.contentView.width - 40;
    self.dateLabel.centerY = self.memberLabel.centerY;
    
    self.amountLabel.centerX = (self.dateLabel.left - self.memberLabel.right) / 2 + self.memberLabel.right;
    self.amountLabel.centerY = self.memberLabel.centerY;
    
    self.tableView.width = self.contentView.width;
    self.tableView.height = self.contentView.height - 46;
    self.tableView.left = 0;
    self.tableView.bottom = self.contentView.height;
}

- (void)loadDataFromServer
{
    if(self.loading)
    {
        return;
    }
    if(self.hasMore == NO)
    {
        return;
    }
    self.loading = YES;
    @weakify(self);
    [CYStandardDataAPI user_projectInvestLogWithId:self.fid page_no:@(self.page_no) complete:^(CYResponse *response) {
        @strongify(self);
        if(response.code == 0)
        {
            self.hasMore = NO;
            NSMutableArray *cells = [NSMutableArray array];
            if([response.content isKindOfClass:[NSArray class]])
            {
                self.hasMore = [response.content count] >= CY_API_PAGE_SIZE;
                self.page_no ++;
                for(NSDictionary *member in response.content)
                {
                    if([member isKindOfClass:[NSDictionary class]])
                    {
                        CYFinanceMemberCellObject *cell = [CYFinanceMemberCellObject objectWithCellClass:[CYFinanceMemberCell class]];
                        cell.member = SAFE_STRING(member[@"username"]);
                        cell.amount = [NSString stringWithFormat:@"%@元", [[SAFE_NUMBER(member[@"invSucc"]) amountString] componentsSeparatedByString:@"."].firstObject];
                        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[SAFE_NUMBER(member[@"addTime"]) doubleValue] / 1000.0];
                        cell.date = [date cy_stringWithFormat:@"yyyy-MM-dd"];
                        cell.time = [date cy_stringWithFormat:@"HH:mm:ss"];
                        [cells addObject:cell];
                    }
                }
            }
            [self.tableModel addObjectsFromArray:cells];
            [self.tableView reloadData];
            self.loading = NO;
        }
        else
        {
            if(self.errorShowed == NO)
            {
                [SVProgressHUD showErrorWithStatus:response.message];
                self.errorShowed = YES;
            }
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                @strongify(self);
                self.loading = NO;
            });
        }
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if(scrollView == self.tableView)
    {
        if(scrollView.contentSize.height - scrollView.contentOffset.y < scrollView.height * 2)
        {
            [self loadDataFromServer];
        }
    }
}

#pragma mark - Private Method
- (UILabel *)titleLabelWithText:(NSString *)text
{
    UILabel *label = [[UILabel alloc] init];
    label.text = text;
    label.font = [UIFont systemFontOfSize:14];
    label.textColor = HEXCOLOR(@"444444");
    [label sizeToFit];
    return label;
}

@end
